/**
/**
 * JAVACC DEMO 1.0
 */

package freemarker.core;

import java.util.Map;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModelException;

/**
 * description:  
 * @author Hou Dayu 创建时间：2016-7-28
 */
public abstract class AbstractDirective implements TemplateDirectiveModel {

	String getTemplatePath(Environment env) {
		String templateName = env.getTemplate().getName();
		int lastSlash = templateName.lastIndexOf('/');
		return lastSlash == -1 ? "" : templateName.substring(0, lastSlash + 1);
	}

	String getRequiredParam(Map params, String key) throws TemplateException {
		Object value = params.get(key);
		if ((value == null) || (value.toString().trim().length() == 0)) {
			throw new TemplateModelException("not found required parameter:" + key + " for directive " + toString());
		}

		return value.toString();
	}

	String getParam(Map params, String key, String defaultValue) throws TemplateException {
		Object value = params.get(key);
		return value == null ? defaultValue : value.toString();
	}

	public String toString() {
		return "<@" + getDirectiveName() + ">";
	}

	public abstract String getDirectiveName();

}
